package com.rpay.dao.config;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import com.google.common.collect.Lists;
import com.rpay.common.factory.YamlPropertySourceFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;
import java.util.List;

/**
 * @author jonnyLee
 */
@Configuration
@PropertySource(value = "classpath:dao-${start.env}.yml",factory = YamlPropertySourceFactory.class)
public class DataBaseConfiguration {

	@Primary
	@Bean(name="writeDataSource")
	@ConfigurationProperties("spring.datasource.druid.write")
	public DataSource writeDataSource() {
		DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
		WallConfig wallConfig = new WallConfig();
		wallConfig.setMultiStatementAllow(true);
		WallFilter wallFilter = new WallFilter();
		wallFilter.setConfig(wallConfig);
		List<Filter> filterList = Lists.newArrayList();
		filterList.add(wallFilter);
		dataSource.setProxyFilters(filterList);
		return dataSource;
	}

	@Bean(name="readDataSource")
	@ConfigurationProperties("spring.datasource.druid.read")
	public DataSource readDataSource() {
		return DruidDataSourceBuilder.create().build();
	}


}